//
//  DLShareView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2019/1/3.
//  Copyright © 2019 delongzhitou. All rights reserved.
//

#import "DLShareView.h"

@interface DLShareView()

@property (nonatomic, strong) UIView *contentBackgroundView;
@property (nonatomic, strong) UIButton *closeButton;

@property (nonatomic, strong) UIImageView *shareImageView;

@end

@implementation DLShareView

+ (instancetype)initWithPopViewWithSuperView:(UIView *)view{
    DLShareView *popView = [[DLShareView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [view addSubview:popView];
    [popView showAnimation:YES];
    return popView;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self layoutSubControls];
    }
    return self;
}

- (void)layoutSubControls {
    
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSelfAction)]];
    [self addSubview:self.contentBackgroundView];
    _contentBackgroundView.size = CGSizeMake(kScreenWidth, kAutoWidth(213));
    _contentBackgroundView.top = kScreenHeight;
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kAutoWidth(168.5))];
    contentView.backgroundColor = kDLViewNormalBgColor;
    [_contentBackgroundView addSubview:contentView];
    
    UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kAutoWidth(21), kScreenWidth, kAutoWidth(15))];
    topLabel.textColor = kColorRGB(140, 140, 110);
    topLabel.text = @"分图片享到";
    topLabel.font = [UIFont systemFontOfSize:kAutoWidth(15)];
    topLabel.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:topLabel];
    
    CGFloat tempW = kAutoWidth(47);
    CGFloat btnHeight = kAutoWidth(70);
    NSArray *imageArr = @[@"icon-qq",@"icon-qqkongjian",@"icon-weixin",@"icon-pengyouquan"];
    NSArray *titleArr = @[@"QQ",@"QQ空间",@"微信",@"朋友圈"];
    for (int i=0; i<imageArr.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(kAutoWidth(26.5)+(tempW+kAutoWidth(22))*i, topLabel.bottom+kAutoWidth(19), tempW, btnHeight)];
        btn.tag = 1000+i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tempW, tempW)];
        imageView.image = [UIImage imageNamed:imageArr[i]];
        [btn addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.bottom+kAutoWidth(6), tempW, kAutoWidth(12))];
        label.textColor = kColorRGB(140, 140, 110);
        label.text = titleArr[i];
        label.font = [UIFont systemFontOfSize:kAutoWidth(12)];
        label.textAlignment = NSTextAlignmentCenter;
        [btn addSubview:label];
        
        [contentView addSubview:btn];
    }
    
    UIImageView *doct = [[UIImageView alloc] initWithFrame:CGRectMake(0, kAutoWidth(143), kAutoWidth(6), kAutoWidth(6))];
    doct.centerX = kScreenWidth/2;
    doct.backgroundColor =  kColorRGB(140, 140, 110);
    doct.layer.cornerRadius = kAutoWidth(6)/2;
    doct.clipsToBounds = YES;
    [_contentBackgroundView addSubview:doct];
    
    _closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0,contentView.bottom , kScreenWidth, _contentBackgroundView.height-contentView.height)];
    _closeButton.backgroundColor = [UIColor whiteColor];
    [_closeButton setTitle:@"取消分享" forState:UIControlStateNormal];
    [_closeButton setTitleColor: kColorRGB(140, 140, 110) forState:UIControlStateNormal];
    _closeButton.titleLabel.font = [UIFont systemFontOfSize:kAutoWidth(15)];
    [_contentBackgroundView addSubview:_closeButton];
    [_closeButton addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *shareImageView = [UIImageView new];
    shareImageView.frame = CGRectMake(kAutoWidth(40), kScreenSafeAreaHeight - _contentBackgroundView.height - kAutoWidth(400), kScreenWidth - kAutoWidth(80), kAutoWidth(380));
    shareImageView.contentMode = UIViewContentModeScaleAspectFit;
    shareImageView.backgroundColor = [UIColor clearColor];
    shareImageView.layer.cornerRadius = 4;
    shareImageView.clipsToBounds = YES;
    [self addSubview:shareImageView];
    self.shareImageView =  shareImageView;
}
- (void) setThirdShareType:(DLShareType)thirdShareType {
    _thirdShareType = thirdShareType;
    switch (self.thirdShareType) {
        case DLShareType_newsFlashes:
        {
            self.shareImageView.hidden = NO;
        }
            break;
            
        default:
            break;
    }
}
- (void) setShareImage:(UIImage *)shareImage {
    _shareImage = shareImage;
    self.shareImageView.image = shareImage;
}


- (void) btnClick:(UIButton *)button {
    [self dismissAnimation:YES];
    
    
    [[DLShareSDKManager share] shareWithTag:button.tag - 1000 andUrl:@"" andImage:self.shareImage andTitle:@"分享给你一条快讯" andContent:@"快来看看吧！" andShareFinallTipBlock:^(NSString * _Nonnull tip) {
        
    }];
    
}

- (void)closeBtnClick {
    [self dismissAnimation:YES];
}
- (void)tapSelfAction {
    [self dismissAnimation:YES];
}
- (UIView *)contentBackgroundView {
    if (!_contentBackgroundView) {
        _contentBackgroundView = [[UIView alloc] init];
        _contentBackgroundView.backgroundColor = [UIColor whiteColor];
        _contentBackgroundView.layer.cornerRadius = 4;
        _contentBackgroundView.layer.masksToBounds = YES;
    }
    return _contentBackgroundView;
}

- (void)showAnimation:(BOOL)animation {
    self.alpha = 0;
    self.contentBackgroundView.top = kScreenHeight;
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 1;
        self.contentBackgroundView.top = kScreenHeight-kAutoWidth(213) ;
    }];
}

- (void)dismissAnimation:(BOOL)animation {
    
    self.alpha = 1;
    [UIView animateWithDuration:0.25 animations:^{
         self.alpha = 0;
         self.contentBackgroundView.top = kScreenHeight;
    } completion:^(BOOL finished) {
        self.contentBackgroundView.top = kScreenHeight;
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
